package cn.iocoder.yudao.module.shop.footprint.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.footprint.service.AppFootprintBaseService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.footprint.dal.dataobject.ShopFootprintDO;
import cn.iocoder.yudao.module.shop.footprint.dal.mysql.ShopFootprintMapper;
import cn.iocoder.yudao.module.shop.footprint.vo.AppShopFootprintVO;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopInfoDO;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import cn.iocoder.yudao.module.shop.info.vo.ShopInfoSimpleRespVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 店铺浏览记录 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AppShopFootprintServiceImpl extends AppFootprintBaseService<ShopInfoSimpleRespVO, ShopFootprintDO, AppShopFootprintVO> implements AppShopFootprintService {
    @Resource
    private ShopFootprintMapper shopFootprintMapper;

    @Resource
    private ShopInfoService shopInfoService;

    @Override
    public ShopInfoSimpleRespVO getSnapshot(Long bizId) {
        ShopInfoDO shopInfoDO = shopInfoService.getInfo(bizId);
        return BeanUtils.toBean(shopInfoDO, ShopInfoSimpleRespVO.class);
    }

    @Override
    public ShopFootprintMapper getInjectedMapper() {
        return shopFootprintMapper;
    }

    @Override
    public PageResult<AppShopFootprintVO> getPage(Integer current, Integer size, Long userId) {
        QueryWrapper<ShopFootprintDO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("recycled", false);
        PageResult<ShopFootprintDO> page = shopFootprintMapper.selectPage(current, size, wrapper);
        if (CollUtil.isEmpty(page.getList())) {
            return PageResult.empty();
        }
        return new PageResult<>(page.getList().stream().map(this::convertToViewObject).toList(), page.getTotal());
    }

}
